
#include <stdio.h>  // for printf

#include <unistd.h>  // for sleep

#include "ohos_init.h"  // for SYS_RUN
#include "cmsis_os2.h"

#include "hi_wifi_api.h"

#include "lwip/ip_addr.h" // SDK提供的wifi功能接口
#include "lwip/netifapi.h"  //协议栈IP地址操作接口


static struct netif *g_lwip_netif = NULL;  //IP层网络接口，在此接口上部署IP地址

/* clear netif's ip, gateway and netmask */
//清空指定网络接口的IP地址、掩码和网关地址
void hi_sta_reset_addr(struct netif *pst_lwip_netif)
{
    ip4_addr_t st_gw;
    ip4_addr_t st_ipaddr;
    ip4_addr_t st_netmask;
    printf("%s %d \r\n", __FILE__, __LINE__);
    if (pst_lwip_netif == NULL) {
        printf("hisi_reset_addr::Null param of netdev\r\n");
        return;
    }

    IP4_ADDR(&st_gw, 0, 0, 0, 0);
    IP4_ADDR(&st_ipaddr, 0, 0, 0, 0);
    IP4_ADDR(&st_netmask, 0, 0, 0, 0);

    netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw);
}

//收到wifi通告以后的回调处理
void wifi_wpa_event_cb(const hi_wifi_event *hisi_event)
{
    if (hisi_event == NULL)
        return;

    switch (hisi_event->event) {
	    //热点扫描结束
        case HI_WIFI_EVT_SCAN_DONE:
            printf("WiFi: Scan results available\n");
            break;
	    //wifi已连接，开始dhcp动态获取IP地址
        case HI_WIFI_EVT_CONNECTED:
            printf("WiFi: Connected\n");
            netifapi_dhcp_start(g_lwip_netif);
            break;
	    //wifi断开连接，停止dhcp过程，并清空已获取到的IP地址等信息
        case HI_WIFI_EVT_DISCONNECTED:
            printf("WiFi: Disconnected\n");
            netifapi_dhcp_stop(g_lwip_netif);
            hi_sta_reset_addr(g_lwip_netif);
            break;
	    //wifi连接超时
        case HI_WIFI_EVT_WPS_TIMEOUT:
            printf("WiFi: wps is timeout\n");
            break;
        default:
            break;
    }
}

//开始启动wifi连接过程
int hi_wifi_start_connect(void)
{
    int ret;
    errno_t rc;
    hi_wifi_assoc_request assoc_req = {0};

    /* copy SSID to assoc_req */
    //热点名称，请修改成你手机热点名称或者家用路由器等热点的名称
    rc = memcpy_s(assoc_req.ssid, HI_WIFI_MAX_SSID_LEN + 1, "lengqinjie", 10); /* 9:ssid length */
    if (rc != EOK) {
        printf("%s %d \r\n", __FILE__, __LINE__);
        return -1;
    }

    /*
     * OPEN mode
     * for WPA2-PSK mode:
     * set assoc_req.auth as HI_WIFI_SECURITY_WPA2PSK,
     * then memcpy(assoc_req.key, "12345678", 8).
     */
    //热点加密方式，采用PSK加密(预共享秘钥)
    assoc_req.auth = HI_WIFI_SECURITY_WPA2PSK;

    /* 热点密码,请修改成上述热点名称对应的密码 */
    memcpy(assoc_req.key, "lengqinjie2", 11);


    //调用SDK进行wifi连接过程
    ret = hi_wifi_sta_connect(&assoc_req);
    if (ret != HISI_OK) {
        printf("%s %d \r\n", __FILE__, __LINE__);
        return -1;
    }
    printf("%s %d \r\n", __FILE__, __LINE__);
    return 0;
}

//本开发板联网范例程序
void sta_demo(void)
{
    int ret;
    char ifname[WIFI_IFNAME_MAX_SIZE + 1] = {0};
    int len = sizeof(ifname);
    unsigned int  num = WIFI_SCAN_AP_LIMIT;

    //启动wifi模组，会生成无线网络接口，例如 "wlan 0"
    ret = hi_wifi_sta_start(ifname, &len);
    if (ret != HISI_OK) {
        printf("%s %d \r\n", __FILE__, __LINE__);
        return;
    }

    /* register call back function to receive wifi event, etc scan results event,
     * connected event, disconnected event.
     */
    // 注册wifi事件通告处理，用于接收wifi事件通告
    ret = hi_wifi_register_event_callback(wifi_wpa_event_cb);
    if (ret != HISI_OK) {
        printf("register wifi event callback failed\n");
    }

    /* acquire netif for IP operation */
    //根据网络接口名称获取网络接口描述符
    g_lwip_netif = netifapi_netif_find(ifname);
    if (g_lwip_netif == NULL) {
        printf("%s: get netif failed\n", __FUNCTION__);
        return ;
    }

    /* start scan, scan results event will be received soon */
    //启动wifi热点扫描
    ret = hi_wifi_sta_scan();
    if (ret != HISI_OK) {
        printf("%s %d \r\n", __FILE__, __LINE__);
        return ;
    }

    //等待5秒后再查询扫描结果
    sleep(5);   /* sleep 5s, waiting for scan result. */

    //用来存储扫描结果的内存块
    hi_wifi_ap_info *pst_results = malloc(sizeof(hi_wifi_ap_info) * WIFI_SCAN_AP_LIMIT);
    if (pst_results == NULL) {
        printf("%s %d \r\n", __FILE__, __LINE__);
        return ;
    }

    //存放扫描结果
    ret = hi_wifi_sta_scan_results(pst_results, &num);
    if (ret != HISI_OK) {
        printf("%s %d \r\n", __FILE__, __LINE__);
        free(pst_results);
        return ;
    }

    //遍历wifi热点扫描结果，并输出到串口显示
    for (unsigned int loop = 0; (loop < num) && (loop < WIFI_SCAN_AP_LIMIT); loop++) {
        printf("SSID: %s\n", pst_results[loop].ssid);
    }
    //释放扫描结果
    free(pst_results);

    /* if received scan results, select one SSID to connect */
    //使用指定的用户名和密码连接，目前硬编码到函数内了，请根据实际情况进行调整
    ret = hi_wifi_start_connect();
    if (ret != 0) {
        printf("%s %d \r\n", __FILE__, __LINE__);
        return ;
    }


    return;

}


SYS_RUN(sta_demo);

